package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.model.vx.vo.WxCatalogCurrentRespVO;
import com.cskaoyan.model.vx.vo.WxCatalogRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: WxCatalogServiceImpl
 * @Author: WangEnzhao
 * @Description: 微信商品分类
 * @CreateTime: 2021/12/19:34:00
 */
@Service
public class WxCatalogServiceImpl implements WxCatalogService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;


    /***
    * @Author: WangEnzhao
    * @Description: 查询所有类目
    * @CreateTime: 19:51 12/2/2021
    * @Param: []
    * @return: com.cskaoyan.model.vx.vo.WxCatalogRespVO
    */
    @Override
    public WxCatalogRespVO categoryIndex() {
        //返回的参数
        WxCatalogRespVO wxCatalogRespVO = new WxCatalogRespVO();

        //查询所有父类目
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(0);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        wxCatalogRespVO.setCategoryList(marketCategories);

        //当前类目为第一个
        MarketCategory marketCategory = marketCategories.get(0);
        wxCatalogRespVO.setCurrentCategory(marketCategory);

        //当前子类目查询
        MarketCategoryExample marketCategoryExampleSub = new MarketCategoryExample();
        marketCategoryExampleSub.createCriteria().andPidEqualTo(marketCategory.getId());
        List<MarketCategory> marketCategoriesSub = marketCategoryMapper.selectByExample(marketCategoryExampleSub);
        wxCatalogRespVO.setCurrentSubCategory(marketCategoriesSub);

        return wxCatalogRespVO;
    }

    /***
    * @Author: WangEnzhao
    * @Description: 查询当前类目
    * @CreateTime: 19:51 12/2/2021
    * @Param: []
    * @return: com.cskaoyan.model.vx.vo.WxCatalogCurrentRespVO
    */
    @Override
    public WxCatalogCurrentRespVO categoryCurrent(Integer id) {
        //要返回的数据
        WxCatalogCurrentRespVO wxCatalogCurrentRespVO = new WxCatalogCurrentRespVO();
        if(id == null){
            return wxCatalogCurrentRespVO;
        }

        //根据id查询当前类目
        MarketCategory marketCategory = marketCategoryMapper.selectMarketCategoryById(id);
        wxCatalogCurrentRespVO.setCurrentCategory(marketCategory);

        //根据pid查询子目录
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        marketCategoryExample.createCriteria().andPidEqualTo(id);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        wxCatalogCurrentRespVO.setCurrentSubCategory(marketCategories);
        return wxCatalogCurrentRespVO;
    }
}
